<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="primary" @click="handleExport">导出</el-button>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="dataList2"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.device_id"
                placeholder="设备编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-select filterable v-model="listQuery.sex" placeholder="性别">
                <el-option
                  v-for="item in sexList"
                  :key="item.key"
                  :label="item.val"
                  :value="item.key"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="用户昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model.number="listQuery.user_code"
                placeholder="用户编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.ip"
                placeholder="IP"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          v-loading="listLoading"
          ref="dataList"
          :data="menuList"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column prop="user_code" label="用户编号"></el-table-column>
          <el-table-column prop="nickname" label="昵称"></el-table-column>
          <el-table-column prop="head_picture" label="头像">
            <template slot-scope="scope">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img
                  :src="scope.row.head_picture"
                  alt=""
                  style="max-width: 200px;max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.head_picture"
                  alt=""
                  style="width: 50px;"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="sex" label="性别">
            <template slot-scope="scoped">
              <el-tag type="primary" v-if="scoped.row.sex === 1">男</el-tag>
              <el-tag type="warning" v-if="scoped.row.sex === 2">女</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="client_type" label="系统"></el-table-column>
          <el-table-column prop="device_id" label="设备编号"></el-table-column>
          <el-table-column
            prop="device_name"
            label="设备名称"
          ></el-table-column>
          <el-table-column prop="ip" label="IP"></el-table-column>
          <el-table-column prop="add_time" label="上线时间"></el-table-column>
          <el-table-column prop="state" label="设备状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.state === 1" type="success">正常</el-tag>
              <el-tag v-if="scope.row.state === 0" type="danger">封禁</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="ip_state" label="IP状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.ip_state === 1" type="success"
                >正常</el-tag
              >
              <el-tag v-if="scope.row.ip_state === 0" type="danger"
                >封禁</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="190px">
            <template slot-scope="scope">
              <el-button
                type="danger"
                v-if="scope.row.state === 1"
                size="mini"
                icon="el-icon-circle-close"
                @click="handleBanDevice(scope.row)"
                plain
                >封设备
              </el-button>
              <el-button
                type="info"
                v-if="scope.row.state === 0"
                size="mini"
                icon="el-icon-circle-close"
                @click="handleCancelBanDevice(scope.row)"
                plain
                >解封设备
              </el-button>
              <el-button
                type="danger"
                v-if="scope.row.ip_state === 1"
                size="mini"
                icon="el-icon-circle-close"
                @click="handleBanIp(scope.row)"
                plain
                >封IP
              </el-button>
              <el-button
                type="info"
                v-if="scope.row.ip_state === 0"
                size="mini"
                icon="el-icon-circle-close"
                @click="handleCancelBanIp(scope.row)"
                plain
                >解封IP
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- ban弹框 -->
    <el-dialog
      title="请输入封禁原因"
      :visible.sync="banDialogVisible"
      @close="handleDialogCloseBan('banReasonForm')"
    >
      <el-form :model="banReasonForm" ref="banReasonForm" class="demo-ruleForm">
        <el-form-item
          label=""
          prop="reason"
          :rules="[{ required: true, message: '封禁理由不能为空' }]"
        >
          <el-input type="text" v-model="banReasonForm.reason"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="banDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmBan('banReasonForm')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 解封设备 -->
    <el-dialog title="信息" :visible.sync="cancelBanDialogVisible">
      <span style="">确定执行此操作？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBanDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmCancelBan">确 定</el-button>
      </span>
    </el-dialog>
    <!-- banIP弹框 -->
    <el-dialog
      title="请输入封禁原因"
      :visible.sync="banIpDialogVisible"
      @close="handleDialogClose('banReasonForm')"
    >
      <el-form :model="banReasonForm" ref="banReasonForm" class="demo-ruleForm">
        <el-form-item
          label=""
          prop="reason"
          :rules="[{ required: true, message: '封禁理由不能为空' }]"
        >
          <el-input type="text" v-model="banReasonForm.reason"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="banIpDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmBanIp('banReasonForm')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 解封IP -->
    <el-dialog title="信息" :visible.sync="cancelBanIpDialogVisible">
      <span style="">确定执行此操作？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBanDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmCancelBanIp">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  postBanDevice,
  postBanIpDevice,
  getExport
} from "@/api/member/device";

export default {
  data() {
    return {
      row_id: undefined,
      // 封禁原因表单
      banReasonForm: {
        reason: ""
      },
      cancelBanIpDialogVisible: false,
      banIpDialogVisible: false,
      cancelBanDialogVisible: false,
      banDialogVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        sex: undefined,
        ip: undefined,
        nickname: undefined,
        user_code: undefined,
        // 设备ID
        device_id: undefined
      },
      sexList: [
        { key: 1, val: "男" },
        { key: 2, val: "女" }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page)
        urlData = urlData + `page=${this.listQuery.page}&`;
      if (this.listQuery.perPage)
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      if (this.listQuery.user_code === 0 || this.listQuery.user_code)
        urlData = urlData + `&user_code=${this.listQuery.user_code}`;
      if (this.listQuery.nickname === 0 || this.listQuery.nickname)
        urlData = urlData + `&nickname=${this.listQuery.nickname}`;
      if (this.listQuery.sex) urlData = urlData + `&sex=${this.listQuery.sex}`;
      if (this.listQuery.ip === 0 || this.listQuery.ip)
        urlData = urlData + `&ip=${this.listQuery.ip}`;
      if (this.listQuery.device_id)
        urlData += `&device_id=${this.listQuery.device_id}`;

      listData(urlData).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
      });
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.sex = undefined;
      this.listQuery.ip = undefined;
      this.listQuery.nickname = undefined;
      this.listQuery.user_code = undefined;
      this.listQuery.device_id = undefined;
      this.getList();
    },
    // 封禁设备
    handleBanDevice(row) {
      console.log(row, "row");
      console.log(this.row_id, "id");
      this.row_id = row.id;

      this.banDialogVisible = true;
    },
    // 封禁IP
    handleBanIp(row) {
      this.row_id = row.id;
      this.banIpDialogVisible = true;
    },
    // 解封设备
    handleCancelBanDevice(row) {
      this.row_id = row.id;
      console.log(this.row_id, "id123");
      this.cancelBanDialogVisible = true;
    },
    // 解封IP
    handleCancelBanIp(row) {
      this.row_id = row.id;
      this.cancelBanIpDialogVisible = true;
    },
    // 确认封禁
    confirmBan(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let obj = {
            state: 0,
            remark: this.banReasonForm.reason
          };
          postBanDevice(this.row_id, obj).then(response => {
            if (response.code === 200) {
              this.getList();
              this.banDialogVisible = false;
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    //解封设备的确定
    confirmCancelBan() {
      let obj = {
        state: 1
      };
      postBanDevice(this.row_id, obj).then(response => {
        if (response.code === 200) {
          this.getList();
          this.cancelBanDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.$message({
            message: response.message,
            type: "error"
          });
        }
      });
    },
    //封禁IP
    confirmBanIp(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let obj = {
            ip_state: 0,
            remark: this.banReasonForm.reason
          };
          postBanIpDevice(this.row_id, obj).then(response => {
            if (response.code === 200) {
              this.getList();
              this.banIpDialogVisible = false;
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    //解封IP
    confirmCancelBanIp() {
      let obj = {
        ip_state: 1
      };
      postBanIpDevice(this.row_id, obj).then(response => {
        if (response.code === 200) {
          this.getList();
          this.cancelBanIpDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.$message({
            message: response.message,
            type: "error"
          });
        }
      });
    },
    // 重置封禁理由
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      this.banReasonForm.reason = "";
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
        this.$refs[formName].resetFields(); //重置表单
      }
    },
    handleDialogCloseBan(formName) {
      this.banReasonForm.reason = "";
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
        this.$refs[formName].resetFields(); //重置表单
      }
    },
    handleExport() {
      let obj = {
        user_code: undefined,
        ip: undefined,
        nickname: undefined,
        sex: undefined,
        device_id: undefined
      };
      for (let key in obj) {
        obj[key] = this.listQuery[key];
      }

      getExport(obj).then(res => {
        if (res.code === 200) {
          window.location.href = res.data.uri;
        }
      });
    }
  }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
